import {useGameStore} from '@/store/game'

export function createCard(row, col, isAble = true) {
  const suits = ['spade', 'heart', 'diamond', 'club']
  return {
    suit: suits[Math.floor(Math.random() * suits.length)],
    row: row,
    col: col,
    isChanging: false,
    isHighlighted: false,
    isMatched: false,
    isAble: isAble,
    specialType: 'none'
  };
}

export function initGame(size) {
  const gameStore = useGameStore();
  gameStore.games.board = []
  gameStore.games.score = 0
  gameStore.games.comboCount = 0
  gameStore.games.isProcessing = true
  gameStore.games.size = size
  gameStore.games.effectChainDepth = 0
  
  // 初始化棋盘 - 确保没有初始匹配
  const newBoard = [];
  const suits = ['spade', 'heart', 'diamond', 'club'];
  
  // 创建初始棋盘，确保没有匹配
  for (let row = 0; row < size; row++) {
    newBoard[row] = [];
    for (let col = 0; col < size; col++) {
      // 避免创建初始匹配
      let suit;
      if (row > 1 && newBoard[row-1][col] && newBoard[row-2][col]) {
        // 避免垂直匹配
        const prevSuit1 = newBoard[row-1][col].suit;
        const prevSuit2 = newBoard[row-2][col].suit;
        const availableSuits = suits.filter(s => s !== prevSuit1 && s !== prevSuit2);
        suit = availableSuits.length > 0 
          ? availableSuits[Math.floor(Math.random() * availableSuits.length)]
          : suits[Math.floor(Math.random() * suits.length)];
      } else if (col > 1 && newBoard[row][col-1] && newBoard[row][col-2]) {
        // 避免水平匹配
        const prevSuit1 = newBoard[row][col-1].suit;
        const prevSuit2 = newBoard[row][col-2].suit;
        const availableSuits = suits.filter(s => s !== prevSuit1 && s !== prevSuit2);
        suit = availableSuits.length > 0 
          ? availableSuits[Math.floor(Math.random() * availableSuits.length)]
          : suits[Math.floor(Math.random() * suits.length)];
      } else {
        suit = suits[Math.floor(Math.random() * suits.length)];
      }
      
      newBoard[row][col] = {
        suit: suit,
        row: row,
        col: col,
        isChanging: false,
        isHighlighted: false,
        isMatched: false,
        specialType: 'none'
      };
    }
  }
  
  gameStore.games.board = newBoard;
  
  // 设置一个短暂的延迟后允许操作
  setTimeout(() => {
    gameStore.games.isProcessing = false;
  }, 500);
}